<template>
  <div class="contact" v-if="is_show_contact">
    <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="hide"
      class="title"
    />
    <van-search
      v-model="search_value"
      shape="round"
      placeholder="请输入搜索关键词"
      class="search"
      clearable
      @search="search(search_value)"
      @clear="search('')"
    />
    <div class="list_box" :style="type == 70? ' bottom: 54px;' : ' bottom: 0px;'">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="我是有底线的"
        @load="onLoad"
      >
        <van-checkbox-group v-model="result">
          <div v-for="item in contact_list" :key="item.username" class="item" @click="selecte(item)">
            <van-image
              v-show="item.email"
              round
              width="40px"
              height="40px"
              :src="item.email"
              style="margin-top: 5px"
            />
            <div class="image" v-show="!item.email">
              {{item.alias.substring(item.alias.length-2)}}
            </div>
            <div class="name_box van-hairline--bottom">
              {{item.alias}}
              <van-checkbox  class="checkbox" :name="item.username"  checked-color="#07c160"></van-checkbox>
            </div>
          </div>
         </van-checkbox-group>
      </van-list>
    </div>
    <div class="btn_box van-hairline--top" v-if="type == 70">
      <span class="span" @click="showSelected">
        已选择: {{this.selected_list.length ? this.selected_list.length + '人 ▲' : ''}}
      </span>
      <van-button type="info" class="btn" block @click="confirm" :disabled="!this.selected_list.length">
        确定 <span>( {{this.selected_list.length}}/50 )</span>
      </van-button>
    </div>
    <transition name="van-slide-up">
      <selected-contact ref="selected_contact" @selectedList="selectedList" @hide="show_selected_contact = false"/>
    </transition>
    <div class="show_selected_contact" v-if="show_selected_contact" @click="hideSelected"></div>
  </div>
</template>

<script>
import selectedContact from './selected_contact.vue'
export default {
  components:{
    selectedContact
  },
  data(){
    return{
      search_value:'',
      is_show_contact: false,
      title: '联系人',
      type: 60, // 默认60, 60:单选,70:多选
      contact_list:[],
      loading: false,
      finished: false,
      selected_list:[],
      result:[], // 已选择状态,留作为修改回填的状态显示,
      show_selected_contact: false,
      next:null,
      page:0,
      is_deliverTicket:''
    }
  },
  mounted(){
    // dom渲染成功后 监听popstate 事件
    window.addEventListener('popstate',e => {
      if(this.is_show_contact){
        this.is_show_contact = !this.is_show_contact
      }
    },false)
  },
  beforeDestroy(){
    // 页面销毁前 移除popstate 事件
    window.removeEventListener('popstate',e => {
      if(this.is_show_contact){
        this.is_show_contact = !this.is_show_contact
      }
    },false)
  },
  methods:{
    // 关闭以选联系人列表
    hideSelected(){
      this.$refs.selected_contact.hide()
      this.show_selected_contact = false
    },
    // 显示已选择的列表
    showSelected(){
      this.show_selected_contact = true
      this.$refs.selected_contact.show(this.selected_list)
    },
    // 已选择的列表变更
    selectedList(list){
      this.result = []
      this.selected_list = []
      list.forEach(item => {
        this.selected_list.push(item)
        this.result.push(item.username)
      })
    },
    // 确认
    confirm(){
      this.hide()
      this.$emit('contactList',this.selected_list)
    },
    // 选择联系人
    selecte(item){
      if(this.selected_list.length == 50){
        return
      }
      if(this.type == 60){
        // 单选
        if(item.username == this.result[0]){
          // 删除
          this.result = []
          this.selected_list = []
        }else{
          // 添加
          this.result = [item.username]
          this.selected_list = [item]
          this.confirm()
        } 
      }else{
        // 多选
        if(this.result.includes(item.username)){
          // 删除
          this.selected_list.forEach((item,index) => {
            if (this.selected_list[index].username == item.username){
              this.selected_list.splice(index,1);
            }
          });
          this.result.forEach( (id,index) => {
            if (this.result[index] == item.username){
              this.result.splice(index,1);
            }
          })
        }else{
          // 添加
          this.result.push(item.username)
          this.selected_list.push(item)
        }
      }
    },
    // 显示
    show(opt={}){
      this.selected_list = []
      this.result = []
      if(opt.type){
        this.type = opt.type
      }
      if(opt.title){
        this.title = opt.title
      }
      if(opt.is_deliverTicket){
        this.is_deliverTicket = opt.is_deliverTicket
      }
      if(opt.list){
        opt.list.forEach(item => {
           this.selected_list.push(item)
           this.result.push(item.username)
        })
      }
      this.is_show_contact = true
      window.history.pushState(null,null,'#')
    },
    // 关闭
    hide(opt={}){
      this.is_show_contact = false
      if(this.is_deliverTicket != 'deliverTicket'){
        this.$router.go(-1)
      }
    },
    search(search_value){
      this.finished = false;
      this.page = 0
      this.contact_list=[]
      this.onLoad(search_value)
    },
    // 加载列表
    onLoad(search_value=this.search_value) {
      this.loading = true;
      this.page ++ 
      this.$store.dispatch('users',{page:this.page,search:search_value}).then(res=>{
        console.log(res)
        res.data.results.forEach(item=>{
        this.contact_list.push(item)
        })
        this.loading = false;
        this.next = res.data.next
        if(!res.data.next){
          this.finished = true;
        }
      }).catch(err=>{
        this.finished = true;
      })
    }
  }
}
</script>

<style lang="less" scoped>
.contact{
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  z-index: 9999;
  .title{
    position: absolute;
    height: 46px;
    top: 0;
    width: 100%;
  }
  .search{
    position: absolute;
    height: 54px;
    top: 46px;
    width: 100%;
  }
  .list_box{
    position: absolute;
    top: 100px;
    width: 100%;
    overflow: auto;
    .item{
      height: 50px;
      padding: 0px 15px;
      line-height: 50px;
      box-sizing: border-box;
      display: flex;
      .image{
        width: 40px;
        height: 40px;
        background-color: rgb(58, 148, 250);
        margin-top: 5px;
        color: #fff;
        text-align: center;
        line-height: 40px;
        overflow: hidden;
        border-radius: 50%;
        font-size: 14px;
      }
      .name_box{
        flex: 1;
        font-size: 14px;
        font-weight: 500;
        line-height: 50px;
        margin-left: 15px;
        position: relative;
        .checkbox{
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: 0;
        }
      }
    }
  }
  .btn_box{
    position: absolute;
    height: 54px;
    bottom: 0;
    width: 100%;
    line-height: 54px;
    color: rgb(58, 148, 250);
    .span{
      margin-left: 15px;
      font-size: 14px;
    }
    .btn{
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
      width: 80px;
      height: 32px;
      line-height: 32px;
      font-size: 12px;
      padding: 0;
    }
  }
}
.show_selected_contact{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.541);
  z-index: 98;
}
</style>